package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.AdministerRoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 检查组管理
 */
@RestController
@RequestMapping("/administer_role")
public class AdministerRoleController {
    @Reference
    private AdministerRoleService administerRoleService;
    //新增
    @RequestMapping("/add")
    public Result add(@RequestBody Role role){
        try{
            administerRoleService.add(role);
            return new Result(true, MessageConstant.ADD_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_ROLE_FAIL);
        }
    }

    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        return administerRoleService.findPage(pageBean);
    }

    //根据id查询角色
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Role role = administerRoleService.findById(id);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //编辑角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role){
        try{
            administerRoleService.edit(role);
            return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
        }
    }

    //查询所有角色
    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<Role> list = administerRoleService.findAll();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
        }
    }

    //根据id删除角色
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            administerRoleService.delete(id);
            return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
        }
    }
}
